package com.example.dianchiwater;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;

import org.json.JSONException;
import org.json.JSONObject;

public class YinDaoYe extends AppCompatActivity {
    TextView tvWd,tvDt,tvZh,tvGd;

    ImageView Gr,tvFh;


    String TAG = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yin_dao_ye);
        tvWd=findViewById(R.id.tvQw);
        tvDt = findViewById(R.id.tvDt);
        tvZh = findViewById(R.id.tvZh);
        tvFh=findViewById(R.id.tvFh);
        tvGd = findViewById(R.id.tvGd);
        tvGd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent( YinDaoYe.this,Shuiwei.class);
                startActivity(i);
            }
        });
        tvFh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i =new Intent(YinDaoYe.this,MainActivity.class);
                startActivity(i);
                finish();
            }
        });
        tvZh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(YinDaoYe.this,Ziranzaihai_Web.class);
                startActivity(i);
            }
        });

        tvDt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(YinDaoYe.this,DongTai.class);
                startActivity(i);
            }
        });

        tvWd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(YinDaoYe.this,Tianqi_Web.class);
                startActivity(i);
            }
        });
        Gr=findViewById(R.id.tvGr);
        Gr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(YinDaoYe.this, Grzx.class);
                startActivity(i);
            }
        });
        HeConfig.init("HE2109151336361479","6c79ae39afb47fa89235bf7a5e9dd34");
        HeConfig.switchToDevService();
        QWeather.getWeatherNow(YinDaoYe.this, "CN101010100", Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener() {
            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "getWeather onError: " + e);
            }

            @Override
            public void onSuccess(WeatherNowBean weatherBean) {
                Log.i(TAG, "getWeather onSuccess: " + new Gson().toJson(weatherBean));
                //先判断返回的status是否正确，当status正确时获取数据，若status不正确，可查看status对应的Code值找到原因
                if (Code.OK == weatherBean.getCode()) {
                    WeatherNowBean.NowBaseBean now = weatherBean.getNow();
                } else {
                    //在此查看返回数据失败的原因
                    Code code = weatherBean.getCode();
                    Log.i(TAG, "failed code: " + code);
                }
            }
        });
        QWeather.getWeatherNow(YinDaoYe.this, "CN101290101", Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener() {
            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "getWeather onError: " + e);
            }

            @Override
            public void onSuccess(WeatherNowBean weatherBean) {
                String jsonData  = new Gson().toJson(weatherBean);
                Log.i(TAG, "getWeather onSuccess: " + new Gson().toJson(weatherBean));
                //先判断返回的status是否正确，当status正确时获取数据，若status不正确，可查看status对应的Code值找到原因
                String tianqi = null,wendu = null,time = null;
                if (Code.OK == weatherBean.getCode()) {
                    WeatherNowBean.NowBaseBean now = weatherBean.getNow();
                    String JsonNow = new Gson().toJson(weatherBean.getNow());
                    String JsonBasic = new Gson().toJson(weatherBean.getBasic());
                    JSONObject jsonObject = null;
                    JSONObject jsonObject2 =null;
                    try {
                        jsonObject = new JSONObject(JsonNow);
                        jsonObject2 = new JSONObject(JsonBasic);
                        tianqi = jsonObject.getString("text");
                        wendu = jsonObject.getString("temp");
                        time = jsonObject2.getString("updateTime");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                } else {
                    //在此查看返回数据失败的原因
                    Code code = weatherBean.getCode();
                    Log.i(TAG, "failed code: " + code);
                }
//                String wendu2 = wendu+"°C";
//                tvWd=findViewById(R.id.tvWd);
//                tvWd.setText("昆明"+"\n"+"更新时间:"+time+"\n"
//                        +"当前天气："+tianqi+"\n"
//                        +"当前温度："+wendu2);
            }
        });


    }
}